package halberd.filter;

import halberd.DataType;
import halberd.ImgFilter;
import halberd.data.ImgData;
import halberd.data.ImgDataFactory;
import halberd.utils.ConvolutionUtils;

/**
 * 高斯模糊
 * 
 * @author jiang
 *
 */
public class GuassBlurFilter implements ImgFilter {

	public ImgData process(ImgData input) {
		if (!canProcess(input)) {
			return null;
		}
		ImgData result = ImgDataFactory.createImgData(input.getWidth(), input.getHeight());
		guass(input.getDatas(), result.getDatas(), input.getWidth(), input.getHeight());
		result.setDataType(input.getDataType());
		return result;
	}

	private boolean canProcess(ImgData input) {
		if (input == null) {
			return false;
		}
		DataType dt = input.getDataType();
		if (dt == DataType.RGB) {
			return true;
		}
		if (dt == DataType.RGBA) {
			return true;
		}
		if (dt == DataType.GRAY) {
			return true;
		}
		return false;
	}

	private void guass(int[][][] src, int[][][] dst, int w, int h) {
		int[][] guassOperator = { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } };
		ConvolutionUtils.convolution9(guassOperator, src, dst,w,h);
	}
}
